<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Basic usage for LuaSandbox</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="luasandbox.examples.html">« 范例</a></li>
      <li style="float: right;"><a href="class.luasandbox.html">LuaSandbox »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="luasandbox.examples.html">范例</a></li>
    <li>Basic usage for LuaSandbox</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="luasandbox.examples-basic" class="section">
  <h2 class="title">Basic usage for LuaSandbox</h2>
  <p class="para">
   Once you&#039;ve compiled PHP with LuaSandbox support, you can begin using LuaSandbox to safely run user-provided Lua code.
  </p>
  <p class="para">
   <div class="example" id="example-3434">
    <p><strong>示例 #1 Execute some Lua code</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br />$sandbox&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">LuaSandbox</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$sandbox</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">setMemoryLimit</span><span style="color: #007700">(&nbsp;</span><span style="color: #0000BB">50&nbsp;</span><span style="color: #007700">*&nbsp;</span><span style="color: #0000BB">1024&nbsp;</span><span style="color: #007700">*&nbsp;</span><span style="color: #0000BB">1024&nbsp;</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$sandbox</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">setCPULimit</span><span style="color: #007700">(&nbsp;</span><span style="color: #0000BB">10&nbsp;</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//&nbsp;Register&nbsp;some&nbsp;functions&nbsp;in&nbsp;the&nbsp;Lua&nbsp;environment<br /><br /></span><span style="color: #007700">function&nbsp;</span><span style="color: #0000BB">frobnosticate</span><span style="color: #007700">(&nbsp;</span><span style="color: #0000BB">$v&nbsp;</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;[&nbsp;</span><span style="color: #0000BB">$v&nbsp;</span><span style="color: #007700">+&nbsp;</span><span style="color: #0000BB">42&nbsp;</span><span style="color: #007700">];<br />}<br /><br /></span><span style="color: #0000BB">$sandbox</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">registerLibrary</span><span style="color: #007700">(&nbsp;</span><span style="color: #DD0000">'php'</span><span style="color: #007700">,&nbsp;[<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'frobnosticate'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'frobnosticate'</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'output'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;function&nbsp;(&nbsp;</span><span style="color: #0000BB">$string&nbsp;</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"</span><span style="color: #0000BB">$string</span><span style="color: #DD0000">\n"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;},<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'error'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;function&nbsp;()&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;throw&nbsp;new&nbsp;</span><span style="color: #0000BB">LuaSandboxRuntimeError</span><span style="color: #007700">(&nbsp;</span><span style="color: #DD0000">"Something&nbsp;is&nbsp;wrong"&nbsp;</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />]&nbsp;);<br /><br /></span><span style="color: #FF8000">//&nbsp;Execute&nbsp;some&nbsp;Lua&nbsp;code,&nbsp;including&nbsp;callbacks&nbsp;into&nbsp;PHP&nbsp;and&nbsp;into&nbsp;Lua<br /><br /></span><span style="color: #0000BB">$luaCode&nbsp;</span><span style="color: #007700">=&nbsp;&lt;&lt;&lt;EOF<br /></span><span style="color: #DD0000">php.output(&nbsp;"Hello,&nbsp;world"&nbsp;);<br /><br />return&nbsp;"Hi",&nbsp;function&nbsp;(&nbsp;v&nbsp;)<br />&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;php.frobnosticate(&nbsp;v&nbsp;+&nbsp;200&nbsp;)<br />end<br /></span><span style="color: #007700">EOF;<br /><br />list(&nbsp;</span><span style="color: #0000BB">$hi</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$frob&nbsp;</span><span style="color: #007700">)&nbsp;=&nbsp;</span><span style="color: #0000BB">$sandbox</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">loadString</span><span style="color: #007700">(&nbsp;</span><span style="color: #0000BB">$luaCode&nbsp;</span><span style="color: #007700">)-&gt;</span><span style="color: #0000BB">call</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">assert</span><span style="color: #007700">(&nbsp;</span><span style="color: #0000BB">$frob</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">call</span><span style="color: #007700">(&nbsp;</span><span style="color: #0000BB">4000&nbsp;</span><span style="color: #007700">)&nbsp;===&nbsp;[&nbsp;</span><span style="color: #0000BB">4242&nbsp;</span><span style="color: #007700">]&nbsp;);<br /><br /></span><span style="color: #FF8000">//&nbsp;PHP-thrown&nbsp;LuaSandboxRuntimeError&nbsp;exceptions&nbsp;can&nbsp;be&nbsp;caught&nbsp;inside&nbsp;Lua<br /><br /></span><span style="color: #007700">list(&nbsp;</span><span style="color: #0000BB">$ok</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$message&nbsp;</span><span style="color: #007700">)&nbsp;=&nbsp;</span><span style="color: #0000BB">$sandbox</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">loadString</span><span style="color: #007700">(&nbsp;</span><span style="color: #DD0000">'return&nbsp;pcall(&nbsp;php.error&nbsp;)'&nbsp;</span><span style="color: #007700">)-&gt;</span><span style="color: #0000BB">call</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">assert</span><span style="color: #007700">(&nbsp;!</span><span style="color: #0000BB">$ok&nbsp;</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">assert</span><span style="color: #007700">(&nbsp;</span><span style="color: #0000BB">$message&nbsp;</span><span style="color: #007700">===&nbsp;</span><span style="color: #DD0000">'Something&nbsp;is&nbsp;wrong'&nbsp;</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
  </p>
 </div></div></div></body></html>